package FunInterface;

import org.junit.Test;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Random;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.function.Supplier;

// java8的四大内置函数式接口
public class Demo1 {


    @Test
    public void test1(){
        testConsumer(1000,(x) -> System.out.println("消费"+x+"元"));
    }

    /**
     *
     * @param money
     * @param consumer 消费型接口，用于消费参数money的，无返回值。
     */
    public void testConsumer(double money, Consumer<Double> consumer){
        consumer.accept(money);
    }



    @Test
    public void test2(){
        System.out.println(testSupplier(()-> (int)(Math.random()*100)));
    }

    /**
     *
     * @param supplier  供给型接口，用于提供产品的
     * @return
     */
    public int testSupplier(Supplier<Integer> supplier){
        return supplier.get();
    }



    @Test
    public void test3(){
        System.out.println(strHandler("Hello java8",(str) -> str.toUpperCase()));
    }
    /**
     *
     * @param str
     * @param function  函数式接口。
     * @return
     */
    public String strHandler(String str, Function<String,String> function){
        return function.apply(str);
    }


    @Test
    public void test4(){
        List list = filterByMin(Arrays.asList(90, 85, 70, 78, 32), integer -> integer > 70);
        System.out.println(list.toString());
    }

    /**
     *
     * @param intList
     * @param predicate 断言式接口
     * @return
     */
    public List filterByMin(List<Integer> intList, Predicate<Integer> predicate){
        List<Integer> result=new ArrayList<>();
        for (Integer integer: intList) {
            if (predicate.test(integer))
                result.add(integer);
        }
        return result;
    }
}
